﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;

using Husb.Data;
using Husb.DataUtil;

using GroundWater.Data;
using UserTable = GroundWater.Data.UserDataSet.UsersDataTable;
using UserRow = GroundWater.Data.UserDataSet.UsersRow;

namespace GroundWater.DataAccess
{
    public class UserDataAdapter : DataAccessAdapter<UserDataSet, UserTable, UserRow>
    {
        public UserDataAdapter()
        {
            this.TableName = "Users";

            GetAllStoredProc = SqlStatements.Users_FetchAllUsers;
            InsertStoredProc = SqlStatements.Users_InsertUser;
            UpdateStoredProc = SqlStatements.Users_UpdateUser;
            DeleteStoredProc = SqlStatements.Users_DeleteUser;

            GetByIdStoredProc = SqlStatements.Users_FetchById;
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            // UserName, :UserRole, :Password, :Description
            db.AddInParameter(cmd, "UserName", DbType.String, "UserName", DataRowVersion.Current);
            db.AddInParameter(cmd, "UserRole", DbType.String, "UserRole", DataRowVersion.Current);
            db.AddInParameter(cmd, "Password", DbType.String, "Password", DataRowVersion.Current);
            db.AddInParameter(cmd, "Description", DbType.String, "Description", DataRowVersion.Current);
        }

        protected override void PopulateStringIdParameters(Database db, DbCommand cmd, string id)
        {
            db.AddInParameter(cmd, "UserName", DbType.String, id);// DbType.AnsiString
        }

        protected override void PopulateDeleteParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "UserName", DbType.String, "UserName", DataRowVersion.Original);
        }

        public static int InsertAuth(string userId, string menu)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("UserId", DbType.String, userId));
            parameters.Add(new DatabaseParameter("MenuId", DbType.String, menu));

            int cnt = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(SqlStatements.Users_InsertAuthorization, parameters, false, true, "Authorization");
            return cnt;
        }

        public static int DeleteAuth(string userId, string menu)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("UserId", DbType.String, userId));
            parameters.Add(new DatabaseParameter("MenuId", DbType.String, menu));

            int cnt = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(SqlStatements.Users_DeleteAuthorization, parameters, false, true, "Authorization");
            return cnt;
        }

        public static int DeleteAuthByUserId(string userId)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("UserId", DbType.String, userId));
            //parameters.Add(new DatabaseParameter("MenuId", DbType.String, menu));

            int cnt = Husb.DataUtil.DataAccessUtil.ExecuteNonQuery(SqlStatements.Users_DeleteAuthorizationByUser, parameters, false, true, "Authorization");
            return cnt;
        }

        public static DataTable FetchAuth()
        {
            DataTable dt = Husb.DataUtil.DataAccessUtil.GetDataTable(SqlStatements.Users_FetchAuthorizationAll, new List<DatabaseParameter>(), false, true, "Authorization");
            return dt;
        }

        public static DataTable FetchMenusAll()
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("Id", typeof(System.Int32));
            //dt.Columns.Add("Name", typeof(System.String));
            //dt.Columns.Add("Date", typeof(System.String));

            //var menus = new string[] { "水井信息维护", "水源地信息维护", "参数设置", "简化后数据", "原始数据", "水位数据查询", "水位柱状图", "水位曲线图", "电池电压报表", "综合报表", "用户管理", "更改口令", "水位饼状图"};

            //for (var i = 0; i < menus.Length; i++)
            //{
            //    DataRow row = dt.NewRow();
            //    row["Id"] = i;
            //    row["Name"] = menus[i];
            //    //row["Date"] = date.ToString("yyyyMMdd");
            //    dt.Rows.Add(row);
            //}

            DataTable dt = Husb.DataUtil.DataAccessUtil.GetDataTable(SqlStatements.Menus_FetchAll, new List<DatabaseParameter>(), false, false, null);


            return dt;
        }
    }
}
